package com.onpositive.knowledge.model.values;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.knowledge.model.value.audits.AbstractAuditResult;
import com.onpositive.knowledge.model.value.audits.ActualDocumentsAudit;
import com.onpositive.knowledge.model.value.audits.GeneralAuditResult;
import com.onpositive.knowledge.model.value.audits.WikiLinksDisambiguationAuditResult;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.units.ParsedString;

public class ActualDocumentsTransformer extends AuditAssistedTransformer {

	public static final String ID = "actual-documents-transformer";
	private static final long serialVersionUID = -4444012565060915078L;

	public ActualDocumentsTransformer(IHasAuditResult source) {
		super(source);
	}

	@Override
	public DescribedValue[] adjust( Object object, IProperty prop, DescribedValue... original) {
		
		if( !prop.getId().equals(IPropertyConstants.ACTUALITY_PROPERTY_METADATA_ID) )
			return original ;
		
		AbstractAuditResult aResult = this.getAuditResultSource().getAuditResult() ;
		
		ActualDocumentsAudit.OwnAuditResult gRes = null ;		
		
		if( aResult instanceof WikiLinksDisambiguationAuditResult ){
			gRes = (ActualDocumentsAudit.OwnAuditResult) aResult ;
		}		
		else if( aResult instanceof GeneralAuditResult )
		{			
			GeneralAuditResult generalResult = (GeneralAuditResult) aResult ;
			AbstractAuditResult _res = generalResult.getResult( ActualDocumentsAudit.ID ) ;
			if( _res instanceof ActualDocumentsAudit.OwnAuditResult )
				gRes = (ActualDocumentsAudit.OwnAuditResult) _res ;
		}
		if( gRes == null )
			return original ;
		
		if( !(object instanceof IThing) )
			return original ;
		
		IThing thing = (IThing) object ;
		String docName = thing.getId() ;
		
		boolean isActual = gRes.getActualDocumentsSet().contains( docName ) ;
		return new DescribedValue[]{ new DescribedValue( new ParsedString( Boolean.toString(isActual), null ), getId() ) } ;
	}

	@Override
	public String getId() {
		return ID;
	}
}
